/*
 * GuudScreen.cpp
 *
 *  Created on: 2013-1-23
 *      Author: heqiu
 */

#include "GuudScreen.h"

namespace guud {

GuudScreen::GuudScreen(uint32_t uiWidthPixel, uint32_t uiHeightPixel) {
	m_uiWidthPixel = uiWidthPixel;
	m_uiHeightPixel = uiHeightPixel;
	m_pSDL_Surface = SDL_SetVideoMode(m_uiWidthPixel, m_uiHeightPixel, 32,
			SDL_SWSURFACE);
}
void GuudScreen::setCaption(const char * pName) {
	SDL_WM_SetCaption(pName, NULL);
}

void GuudScreen::white() {
	if (m_pSDL_Surface == NULL) {
		return;
	}
	SDL_FillRect(m_pSDL_Surface, &m_pSDL_Surface->clip_rect,
			SDL_MapRGB(m_pSDL_Surface->format, 0xFF, 0xFF, 0xFF));
}
void GuudScreen::show() {
	if (m_pSDL_Surface == NULL) {
		return;
	}
	SDL_Flip(m_pSDL_Surface);
}

void GuudScreen::drawRect(GuudRect & gr, GuudColor & color) {
	SDL_FillRect(m_pSDL_Surface, &gr,
			SDL_MapRGB(m_pSDL_Surface->format, color.m_color.r, color.m_color.g,
					color.m_color.b));
}
void GuudScreen::drawCoordinate(GuudPoint & gp, GuudFont & stFont) {
	{
		SDL_Rect wall;
		wall.x = gp.m_iX;
		wall.y = gp.m_iY;
		wall.w = 50;
		wall.h = 20;
		SDL_FillRect(m_pSDL_Surface, &wall,
				SDL_MapRGB(m_pSDL_Surface->format, 0x00, 0xff, 0xff));
		{
			char buf[64];
			sprintf(buf, "(%d,%d)", gp.m_iX, gp.m_iY);
			GuudColor stColor;
			stColor.setColor(0, 0, 0);
			SDL_Surface *message = TTF_RenderText_Solid(stFont.m_font, buf,
					stColor.m_color);
			SDL_BlitSurface(message, NULL, m_pSDL_Surface, &wall);
			SDL_FreeSurface(message);
		}
	}
}
void GuudScreen::drawStr(GuudPoint & gp, GuudFont & stFont, const char * pStr) {
	{
		SDL_Rect wall;
		wall.x = gp.m_iX;
		wall.y = gp.m_iY;
		wall.w = 50;
		wall.h = 20;
		SDL_FillRect(m_pSDL_Surface, &wall,
				SDL_MapRGB(m_pSDL_Surface->format, 0x00, 0xff, 0xff));
		{
			GuudColor stColor;
			stColor.setColor(0, 0, 0);
			SDL_Surface *message = TTF_RenderText_Solid(stFont.m_font, pStr,
					stColor.m_color);
			SDL_BlitSurface(message, NULL, m_pSDL_Surface, &wall);
			SDL_FreeSurface(message);
		}
	}
}
void GuudScreen::drawButton(GuudPoint & gp, GuudButton & stButton) {
	if (stButton.m_pGuudSurface == NULL) {
		return;
	}
	if (stButton.m_pGuudSurface->m_pSDL_Surface == NULL) {
		return;
	}
	if (stButton.m_bShow) {
		SDL_Rect offset;
		offset.x = gp.m_iX;
		offset.y = gp.m_iY;
		switch (stButton.m_status) {
		case GUUD_BUTTON_STATUS_OUT:
			SDL_BlitSurface(stButton.m_pGuudSurface->m_pSDL_Surface,
					&stButton.m_rectOut, m_pSDL_Surface, &offset);
			break;
		case GUUD_BUTTON_STATUS_IN:
			SDL_BlitSurface(stButton.m_pGuudSurface->m_pSDL_Surface,
					&stButton.m_rectIn, m_pSDL_Surface, &offset);
			break;
		case GUUD_BUTTON_STATUS_PRESSED:
			SDL_BlitSurface(stButton.m_pGuudSurface->m_pSDL_Surface,
					&stButton.m_rectPressed, m_pSDL_Surface, &offset);
			break;
		}
	}
}

void GuudScreen::drawLine(GuudPoint & gpBegin, GuudPoint & gpEnd,
		GuudColor & color) {
	GuudDraw::drawLine(m_pSDL_Surface, gpBegin.m_iX, gpBegin.m_iY, gpEnd.m_iX,
			gpEnd.m_iY, color, m_uiWidthPixel, m_uiHeightPixel);
}

}

